package com.hgyc.mom.mes.websocket.service;

import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageHandler {

    public void handleMessage(String message, Session session) {
        if ("ping".equalsIgnoreCase(message)) {
            send(session, "pong");
        } else {
            log.debug("接收到客户端消息: {}", message);
            // TODO: 可扩展自定义协议处理器
        }
    }

    private void send(Session session, String message) {
        try {
            if (session != null && session.isOpen()) {
                session.getBasicRemote().sendText(message);
            }
        } catch (Exception e) {
            log.warn("发送消息失败", e);
        }
    }
}
